<?php
/**
 * +----------------------------------------------------------------------
 * | 九月科技-ztuc.cn
 * +----------------------------------------------------------------------
 *                      .::::.
 *                    .::::::::.            | AUTHOR: siyu
 *                    :::::::::::           | EMAIL: ztucke@ztuc.cn
 *                 ..:::::::::::'           | DATETIME: 2020/01/31
 *             '::::::::::::'
 *                .::::::::::
 *           '::::::::::::::..
 *                ..::::::::::::.
 *              ``::::::::::::::::
 *               ::::``:::::::::'        .:::.
 *              ::::'   ':::::'       .::::::::.
 *            .::::'      ::::     .:::::::'::::.
 *           .:::'       :::::  .:::::::::' ':::::.
 *          .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *     ...:::           ::::::::::::'              ``::.
 *   ```` ':.          ':::::::::'                  ::::..
 *                      '.:::::'                    ':'````..
 * +----------------------------------------------------------------------
 */
/**
/**
 * Created by PhpStorm.
 * ClickLog: Administrator
 * Date: 2020/3/14
 * Time: 17:18
 */

namespace app\lnadmin\controller;
use app\core\InitController;
use think\facade\View;
use app\lnadmin\model\admin\UserRule as UserRuleModel;

class UserRule extends InitController
{
   // 数据
     public function index_json()
    {
            $model = new UserRuleModel();
        if ($this->request->isAjax()) {
            $count = $model->count();
            $list  = $model->field(['id','pid','name as title','url_name as href','icon as icons','weigh','status','create_time','ismenu as type','openType'])->select();
            $pidMenuList = list_to_level($list);
            $data  = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $pidMenuList,
            ];
            return json($data);
        }
       
    }
 public function buildPidMenu($pid, $list, $level = 0)
    {
        $newList = [];
        foreach ($list as $vo) {
            if ($vo['pid'] == $pid) {
                $level++;
                foreach ($newList as $v) {
                    if ($vo['pid'] == $v['pid'] && isset($v['level'])) {
                        $level = $v['level'];
                        break;
                    }
                }
                $vo['level'] = $level;
                if ($level > 1) {
                    $repeatString = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    $markString   = str_repeat("{$repeatString}├{$repeatString}", $level - 1);
                    $vo['title']  = $markString . $vo['title'];
                }
                //$vo['icon1'] = $vo['icon'];unset($vo['icon']);
                $newList[] = $vo;
                $childList = $this->buildPidMenu($vo['id'], $list, $level);
                !empty($childList) && $newList = array_merge($newList, $childList);
            }

        }
        return $newList;
    }


    // 列表
    public function list()
    {
        return View::fetch('UserRule');
    }

   /**
     * 添加新的角色json_encode($arrayName);
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();  
            $verify = input('_verify', true);
            //验证规则
            if($verify!='0'){
                try{
                    $this->validate($param, 'userRule');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $result = UserRuleModel::create($param);
            if ($result == true) {
            add_admin_log('add', '菜单栏目');// 记录行为日志
                return $this->success('', 0,'添加成功',url('userRule/list'));
            } else {
                $this->error($this->errorMsg);
            }
        }
         $userRule = UserRuleModel::getRule();
        return $this->fetch('saveRule', ['userRule' => list_to_level($userRule)]);
    }

    /**
     * 修改角色
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $verify = input('_verify', true);
            //验证规则
            if($verify!='0'){
                try{
                    $this->validate($param, 'userRule');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $result = UserRuleModel::update($param,['id'=>$param['id']]);
            if ($result == true) {
            add_admin_log('edit', '菜单栏目');// 记录行为日志
                return $this->success('', 0,'修改成功',url('userRule/list'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('saveRule', ['data' => UserRuleModel::where('id', input('get.id'))->find(),'userRule' => list_to_level(UserRuleModel::getRule())]);
    }

    /**
     * 删除角色
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            UserRuleModel::where('pid', input('id'))->count() && $this->error('请先删除子节点');
            UserRuleModel::destroy($param['id']);
            add_admin_log('del', '菜单栏目');// 记录行为日志
            $this->success('删除成功');
        }
    }
    
}